package com.epfresh.supplier.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.ProductConfigs;
import com.epfresh.supplier.entity.Promotion;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class PromotionListAdapter extends BaseAdapter implements View.OnClickListener {

    public Context context;
    ListView lv;
    public List<Promotion> groupList;

    public boolean isDc;


    private AdapterView.OnItemClickListener onItemClickListener;
    private OnBottomClickListener onBottomClickListener;
    private OnNotifyDataSetChangedListener onNotifyDataSetChangedListener;

    public PromotionListAdapter(Context context, List<Promotion> groupList, ListView lv) {
        this.context = context;
        this.groupList = groupList;
        this.lv = lv;
    }



    @Override
    public void notifyDataSetChanged() {
        if (onNotifyDataSetChangedListener != null) {
            onNotifyDataSetChangedListener.OnNotifyDataSetChangedListener(groupList.size());
        }
        super.notifyDataSetChanged();
    }

    public OnNotifyDataSetChangedListener getOnNotifyDataSetChangedListener() {
        return onNotifyDataSetChangedListener;
    }

    public void setOnNotifyDataSetChangedListener(OnNotifyDataSetChangedListener onNotifyDataSetChangedListener) {
        this.onNotifyDataSetChangedListener = onNotifyDataSetChangedListener;
    }

    public OnBottomClickListener getOnBottomClickListener() {
        return onBottomClickListener;
    }

    public void setOnBottomClickListener(OnBottomClickListener onBottomClickListener) {
        this.onBottomClickListener = onBottomClickListener;
    }


   /* @Override
    public int getCount() {
        return groupList == null ? 0 : groupList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }*/


//    public View getView(int groupPosition, View convertView, ViewGroup viewGroup) {
//
//    }


    @Override
    public void onClick(View v) {


        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            int type = 0;
            switch (v.getId()) {
                case R.id.ll_sold_up:
//                case R.id.tv_sold_up:
                    type = 0;
                    if (onBottomClickListener != null) {
                        onBottomClickListener.onBottomClickListener(position, type);
                    }
                    break;
                case R.id.ll_edit:
//                case R.id.tv_edit:
                    type = 1;
                    if (onBottomClickListener != null) {
                        onBottomClickListener.onBottomClickListener(position, type);
                    }
                    break;
                case R.id.ll_edit_price:
//                case R.id.tv_edit_price:
                    type = 2;
                    if (onBottomClickListener != null) {
                        onBottomClickListener.onBottomClickListener(position, type);
                    }
                    break;
                case R.id.rl_detail:
                    if (onBottomClickListener != null) {
                        onBottomClickListener.onBottomClickListener(position, 3);
                    }
                    break;
                case R.id.rl_expend:
                    if (onBottomClickListener != null) {
                        onBottomClickListener.onBottomClickListener(position, 4);
                    }
                    break;
                case R.id.ll_stock:
                    if (onBottomClickListener != null) {
                        onBottomClickListener.onBottomClickListener(position, 6);
                    }
                    break;
                case R.id.ll_delete:

                    if (onBottomClickListener != null) {
                        onBottomClickListener.onBottomClickListener(position, 7);
                    }
                    break;
                case R.id.rl_body:

                    if (onBottomClickListener != null) {
                        onBottomClickListener.onBottomClickListener(position, 8);
                    }
                    break;

                case R.id.tv_finish:

                        Object child = groupList.get(position);

                        if (child != null && child instanceof Promotion && onChildClickListener != null) {
                            onChildClickListener.onChildClickListener(position, 0, (Promotion) child, 0);
                        }

                    break;
                case R.id.v_content:




                        Object child2 = groupList.get(position);
                        if (child2 != null && child2 instanceof Promotion && onChildClickListener != null) {
                            onChildClickListener.onChildClickListener(position, 0, (Promotion) child2, 1);
                        }

                    break;

            }


        }
    }




    @Override
    public int getCount() {
        return groupList == null ? 0 : groupList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int groupPosition, View convertView, ViewGroup parent) {
        ViewChildHolder vh = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_child_product_sold, null, false);

            vh = new ViewChildHolder();

            vh.tvFlag = (TextView) convertView.findViewById(R.id.tv_flag);
            vh.tvPriceUnit = (TextView) convertView.findViewById(R.id.tv_price_unit);
            vh.tvPriceO = (TextView) convertView.findViewById(R.id.tv_price_o);
            vh.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
            vh.tvAmount = (TextView) convertView.findViewById(R.id.tv_amount);
            vh.tvLimit = (TextView) convertView.findViewById(R.id.tv_limit);
            vh.tvFinish = (TextView) convertView.findViewById(R.id.tv_finish);
            vh.tvFinishFlag = (ImageView) convertView.findViewById(R.id.tv_finish_flag);
            vh.vLine = convertView.findViewById(R.id.v_line_1);
            vh.vWhole = convertView.findViewById(R.id.v_content);

            vh.tvPriceO.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            convertView.setTag(vh);
        } else {
            vh = (ViewChildHolder) convertView.getTag();
        }
        vh.tvFinish.setTag(R.id.item_key_position, groupPosition);
        vh.tvFinish.setOnClickListener(this);

        vh.vWhole.setTag(R.id.item_key_position, groupPosition);
        vh.vWhole.setOnClickListener(this);



        Promotion promotion =  groupList.get(groupPosition);

        String type = promotion.getType();

        if (groupPosition != 0) {
            vh.vLine.setVisibility(View.INVISIBLE);
        } else {
            vh.vLine.setVisibility(View.VISIBLE);
        }
        String quantityPerAccount = promotion.getQuantityPerAccount();


        //1促销2秒杀
        if ("1".equals(type)) {
            vh.tvFlag.setText("促销");
            vh.tvFlag.setBackgroundResource(R.drawable.promotion_bg);
            vh.tvLimit.setText("限购：" + quantityPerAccount + promotion.getChargeUnit() + "/天/人");

            vh.tvAmount.setText(promotion.getLimitStr());
        } else if ("2".equals(type)) {
            vh.tvFlag.setBackgroundResource(R.drawable.seckill_bg);
            vh.tvFlag.setText("秒杀");
            vh.tvLimit.setText("限购：" + quantityPerAccount + promotion.getChargeUnit() + "/场/人");
            vh.tvAmount.setText(promotion.getSecKillAmount());
        }

        if (quantityPerAccount == null || "".equals(quantityPerAccount)){
            vh.tvLimit.setVisibility(View.INVISIBLE);
        }else {
            vh.tvLimit.setVisibility(View.VISIBLE);
        }
        vh.tvTime.setText(promotion.getPromotionTime());
        vh.tvPriceO.setText(promotion.getOriginalCost() + "元/" + promotion.getChargeUnit());
        vh.tvPriceUnit.setText(promotion.getPrice() + "元/" + promotion.getChargeUnit());

        vh.tvFinishFlag.setVisibility(View.GONE);

        if (promotion.isOverStatus()){
            vh.tvFinishFlag.setVisibility(View.VISIBLE);
            vh.tvFinish.setVisibility(View.GONE);
        }else {
            vh.tvFinishFlag.setVisibility(View.INVISIBLE);
            vh.tvFinish.setVisibility(View.VISIBLE);
        }
        return convertView;
    }




    public class ViewChildHolder {


        TextView tvFlag;
        TextView tvPriceUnit;
        TextView tvPriceO;
        TextView tvTime;
        TextView tvAmount;
        TextView tvLimit;
        TextView tvFinish;
        ImageView tvFinishFlag;
        View vLine;
        View vWhole;

    }

    OnChildClickListener onChildClickListener;

    public PromotionListAdapter.OnChildClickListener getOnChildClickListener() {
        return onChildClickListener;
    }

    public void setOnChildClickListener(PromotionListAdapter.OnChildClickListener onChildClickListener) {
        this.onChildClickListener = onChildClickListener;
    }

    public interface OnBottomClickListener {

        public void onBottomClickListener(int position, int type);
    }

    public interface OnChildClickListener {

        public void onChildClickListener(int position, int childPosition, Promotion promotion, int type);
    }

    public interface OnNotifyDataSetChangedListener {

        public void OnNotifyDataSetChangedListener(int size);
    }
}
